package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelPageReqDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.channels.Channel;
import java.util.Date;
import java.util.List;
import java.util.Queue;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 分页查询频道信息
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult channelList(ChannelPageReqDto dto) {
        //1.检查参数
        //分页检查
        dto.checkParam();
        //2.分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        //模糊匹配
        if (dto.getName() != null && StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(WmChannel::getName, dto.getName());
        }
        //根据时间倒序排序
        queryWrapper.orderByDesc(WmChannel::getCreatedTime);
        //3.将查询到的数据封装到page中
        page = page(page, queryWrapper);
        //4.返回结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 根据频道id删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult channelDel(Integer id) {
        //1.参数检查
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.检查该频道是否已经禁用
        WmChannel wmChannel = getById(id);
        if (wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (wmChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "只有禁用的频道才能删除");
        }
        //3.频道已禁用可删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 保存频道数据
     *
     * @param wmChannel
     * @return
     */
    public ResponseResult channelSave(WmChannel wmChannel) {
        //1.检查参数
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断该频道是否已存在
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getName, wmChannel.getName());
        WmChannel channel = getOne(queryWrapper);
        if (channel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //3.保存频道数据
        //设置其为非默认频道
        wmChannel.setIsDefault(false);
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private WmNewsMapper wmNewsMapper;

    /**
     * 修改频道数据
     *
     * @param wmChannel
     * @return
     */
    public ResponseResult channelUpdate(WmChannel wmChannel) {
        //1.检查参数
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (wmChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.查询频道是否被引用
        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNews::getChannelId, wmChannel.getId());
        List<WmNews> wmNews = wmNewsMapper.selectList(queryWrapper);
        if (!wmNews.isEmpty()) {
            //频道被引用
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "数据被引用无法禁用频道");
        }
        //3.修改频道数据
        //3.1通过id查出当前数据--channel  对比  传过来的数据  如果不一样说明是编辑，一样说明是点击禁用或者启用
        WmChannel channel = getById(wmChannel.getId());
        //编辑操作
        if (!wmChannel.getName().equals(channel.getName())){
            //判断修改后的频道是否已经存在
            LambdaQueryWrapper<WmChannel> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(WmChannel::getName, wmChannel.getName());
            if (getOne(queryWrapper1)!=null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"修改的数据已在表中存在");
            }
        }
        wmChannel.setCreatedTime(new Date());
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}